package com.example.commonlib.base.view;


import com.example.commonlib.ResourceTable;
import com.example.commonlib.base.annotation.IdRes;
import com.example.commonlib.base.annotation.LayoutRes;
import com.example.commonlib.base.annotation.SuppressLint;
import com.example.commonlib.base.components.StateChangedListenerAdapter;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.jetbrains.annotations.Nullable;


/**
 * Fragment顶级父类 : 添加各种状态(数据错误，数据为空，数据加载中)页的展示，
 * 自定义的MaterialDialog的显示，进度条dialog显示
 * @author linlongxin 2018/3/27.
 */
public abstract class BaseFraction extends Fraction implements ISuperFunction {

    private final HiLogLabel tag = new HiLogLabel(HiLog.LOG_APP,0x00201,BaseFraction.class.getSimpleName());
    private final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,0x00201,"TEST001");
    private static final int ANIMATION_DURATION = 300;
    private int mLayoutResId;
    private boolean isUseStatusPages = false;
    private boolean isShowLoading = true;
    private boolean isShowingContent = false;
    private boolean isShowingError = false;
    private boolean isShowingEmpty = false;
    private AnimatorProperty mShowAnimator;
    private AnimatorProperty mHideAnimator;
    // status page
    private DirectionalLayout mEmptyPage;
    private DirectionalLayout mErrorPage;
    private DirectionalLayout mLoadingPage;
    private Component mRealContent;
    // 当前显示的 page
    private Component mCurrentPage;
    private DirectionalLayout mLoadDataButton;
    private Component mRoot;
    public BaseFraction() {
    }

    @SuppressLint("ValidFragment")
    public BaseFraction(Component fragment) {
        mRoot = fragment;
    }

    @SuppressLint("ValidFragment")
    public BaseFraction(@LayoutRes int layoutResID) {
        this(layoutResID, false);
    }

    @SuppressLint("ValidFragment")
    public BaseFraction(@LayoutRes int layoutResID,  boolean isUseStatusPages) {
        this.mLayoutResId = layoutResID;
        this.isUseStatusPages = isUseStatusPages;
    }

    @Nullable
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, @Nullable ComponentContainer container, @Nullable Intent intent) {
        HiLog.debug(tag,"onCreateView");
        if (isUseStatusPages) {
            onInitPages(scatter, mLayoutResId);
            return mRoot;
        } else {
            if (mLayoutResId != 0) {
                mRoot = scatter.parse(mLayoutResId, container, false);
            }
            return mRoot;
        }
    }

    @Override
    protected void onStart(Intent intent) {
        HiLog.error(label, "*****************BaseFraction onStart******************");
        super.onStart(intent);
    }

    public <V extends Component> V findViewById(@IdRes int resId) {
        return (V) mRoot.findComponentById(resId);
    }

    /**
     * 添加各种状态页
     */
    private void addStatusPage(LayoutScatter inflater, @Nullable ComponentContainer container) {

    }

    @Override
    public void onInitPages(int contentId) {

    }

    @Override
    public void onInitPages(LayoutScatter inflater, int contentId) {
        HiLog.error(label, "***************** onInitPages  ******************");
        mRoot = inflater.parse(ResourceTable.Layout_base_status_page, null, false);
        mRoot.estimateSize(-1, -1);
        mRealContent = inflater.parse(contentId, null, false);
        if (mRoot instanceof ComponentContainer) {
            ((StackLayout) mRoot).addComponent(mRealContent);
        }
        mEmptyPage = findViewById(ResourceTable.Id_empty_page);
        mLoadDataButton = findViewById(ResourceTable.Id_error_to_load_button);
        mErrorPage = findViewById(ResourceTable.Id_error_page);
        mLoadingPage = findViewById(ResourceTable.Id_loading_page);
        mRealContent.setVisibility(Component.FOCUS_ENABLE);
        mCurrentPage = mLoadingPage;
        mLoadDataButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                HiLog.error(label, "***************** ClickedListener  ******************");
                onErrorRetry(v);
            }
        });
    }

    @Override
    public void showEmpty() {
        HiLog.error(label, "***************** showEmpty  ******************");
        HiLog.debug(tag,"showEmpty");
        if (!isShowingEmpty) {
            showView(mEmptyPage);
            isShowingError = false;
            isShowingContent = false;
            isShowLoading = false;
            isShowingEmpty = true;
        }
    }

    @Override
    public void showError() {
        HiLog.error(label, "***************** showError  ******************");
        HiLog.debug(tag,"showError");
        if (!isShowingError) {
            showView(mErrorPage);
            isShowingError = true;
            isShowingContent = false;
            isShowLoading = false;
            isShowingEmpty = false;
        }
    }

    @Override
    public void showLoading() {
        HiLog.error(label, "***************** showLoading  ******************");
        HiLog.debug(tag,"showLoading");
        if (!isShowLoading) {
            showView(mLoadingPage);
            isShowingError = false;
            isShowingContent = false;
            isShowLoading = true;
            isShowingEmpty = false;
        }
    }

    @Override
    public void showContent() {
        HiLog.error(label, "***************** showContent  ******************");
        HiLog.debug(tag,"showContent");
        if (!isShowingContent) {
            showView(mRealContent);
            isShowingContent = true;
            isShowingError = false;
            isShowLoading = false;
            isShowingEmpty = false;
        }
    }


    @Override
    public void onErrorRetry(Component v) {
        showLoading();
    }

    public void showView(Component view) {
        hideViewWithAnimation(mCurrentPage);
        mCurrentPage = view;
        showViewWithAnimation(view);
    }

    /**
     * 展示状态页添加动画
     *
     * @param view
     */
    private void showViewWithAnimation(final Component view) {
        if (mShowAnimator != null) {
            mShowAnimator.cancel();
        }
        view.setVisibility(Component.VISIBLE);
        view.setAlpha(0f);
        mShowAnimator = view.createAnimatorProperty().alpha(1).setDuration(ANIMATION_DURATION);
        mShowAnimator.start();
    }

    /**
     * 隐藏状态页添加动画
     *
     * @param view
     */
    private void hideViewWithAnimation(final Component view) {
        if (mHideAnimator != null) {
            mHideAnimator.setStateChangedListener(null);
            mHideAnimator.cancel();
        }
        view.setAlpha(1f);
        mHideAnimator = view.createAnimatorProperty().alpha(0f).setDuration(ANIMATION_DURATION);
        mHideAnimator.start();
        mHideAnimator.setStateChangedListener(new StateChangedListenerAdapter() {
            @Override
            public void onStop(Animator animator) {
                super.onStop(animator);
                view.setVisibility(Component.FOCUS_ENABLE);
            }
        });
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void onStop() {
        super.onStop();
        if (mShowAnimator != null) {
            mShowAnimator.cancel();
        }
        if (mHideAnimator != null) {
            mHideAnimator.cancel();
        }
    }
}
